﻿using System;
using System.Data;
using System.Collections.Generic;
using Things.Common;
using Things.Model;
using Things.DALFactory;
using Things.IDAL;
namespace Things.BLL
{
	/// <summary>
	/// BuyerInfo
	/// </summary>
	public partial class BuyerInfo
	{
		private readonly IBuyerInfo dal=DataAccess.CreateBuyerInfo();
		public BuyerInfo()
		{}
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string BuyerAccount)
		{
			return dal.Exists(BuyerAccount);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Things.Model.BuyerInfo model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Things.Model.BuyerInfo model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string BuyerAccount)
		{
			
			return dal.Delete(BuyerAccount);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string BuyerAccountlist )
		{
			return dal.DeleteList(BuyerAccountlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Things.Model.BuyerInfo GetModel(string BuyerAccount)
		{
			
			return dal.GetModel(BuyerAccount);
		}

        public string GetUserName(string buyerAccount, int length=11)
        {
            string result = string.Empty;

            if (string.IsNullOrEmpty(buyerAccount) == false)
            {
                Things.BLL.BuyerInfo biBLL = new BLL.BuyerInfo();
                Things.Model.BuyerInfo biModel = biBLL.GetModel(buyerAccount);
                if (biModel != null)
                {
                    if (string.IsNullOrEmpty(biModel.PetName) == false)
                    {
                        return StringRemove(biModel.PetName, length);
                    }
                    if (string.IsNullOrEmpty(biModel.RealName) == false)
                    {
                        return StringRemove(biModel.RealName, length);
                    }

                }
            }

            // 如果没有详细个人信息，就去account作为用户名
            string temp = buyerAccount.Split(new string[] { "@" }, StringSplitOptions.None)[0];

            // 避免用户名过长
            result = StringRemove(temp, 11);

            return result;
        }

        protected string StringRemove(string temp, int length)
        {
            string result = string.Empty;
            // 避免用户名过长
            if (temp.Length > length)
            {
                result = temp.Remove(length);
            }
            else
            {
                result = temp;
            }
            return result;
        }

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Things.Model.BuyerInfo GetModelByCache(string BuyerAccount)
		{
			
			string CacheKey = "BuyerInfoModel-" + BuyerAccount;
			object objModel = Things.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(BuyerAccount);
					if (objModel != null)
					{
						int ModelCache = Things.Common.ConfigHelper.GetConfigInt("ModelCache");
						Things.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Things.Model.BuyerInfo)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Things.Model.BuyerInfo> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Things.Model.BuyerInfo> DataTableToList(DataTable dt)
		{
			List<Things.Model.BuyerInfo> modelList = new List<Things.Model.BuyerInfo>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Things.Model.BuyerInfo model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Things.Model.BuyerInfo();
					if(dt.Rows[n]["BuyerAccount"]!=null && dt.Rows[n]["BuyerAccount"].ToString()!="")
					{
					model.BuyerAccount=dt.Rows[n]["BuyerAccount"].ToString();
					}
					if(dt.Rows[n]["PetName"]!=null && dt.Rows[n]["PetName"].ToString()!="")
					{
					model.PetName=dt.Rows[n]["PetName"].ToString();
					}
					if(dt.Rows[n]["RealName"]!=null && dt.Rows[n]["RealName"].ToString()!="")
					{
					model.RealName=dt.Rows[n]["RealName"].ToString();
					}
					if(dt.Rows[n]["Email"]!=null && dt.Rows[n]["Email"].ToString()!="")
					{
					model.Email=dt.Rows[n]["Email"].ToString();
					}
					if(dt.Rows[n]["MobilePhone"]!=null && dt.Rows[n]["MobilePhone"].ToString()!="")
					{
					model.MobilePhone=dt.Rows[n]["MobilePhone"].ToString();
					}
					if(dt.Rows[n]["Address"]!=null && dt.Rows[n]["Address"].ToString()!="")
					{
					model.Address=dt.Rows[n]["Address"].ToString();
					}
					if(dt.Rows[n]["BuyerIamge"]!=null && dt.Rows[n]["BuyerIamge"].ToString()!="")
					{
						model.BuyerIamge=(byte[])dt.Rows[n]["BuyerIamge"];
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  Method
	}
}

